package com.vf.admin.client.dao;


import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppFamilyMember;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class AppFamilyMemberDao extends BaseDao<AppFamilyMember> {


    public AppFamilyMember findInfo(AppFamilyMember appFamilyMember)
    {
        List<Object> paramList = new ArrayList<>();
        StringBuilder stringBuilder = new StringBuilder("select * from app_family_member where 1=1");
        if(appFamilyMember.getFid()!=null && appFamilyMember.getFid()>0){
            stringBuilder.append(" and fid = ?");
            paramList.add(appFamilyMember.getFid());
        }
        if(appFamilyMember.getUserid()!=null && appFamilyMember.getUserid()>0){
            stringBuilder.append(" and userid = ?");
            paramList.add(appFamilyMember.getUserid());
        }
        if(appFamilyMember.getStatus()!=null){
            stringBuilder.append(" and status = ?");
            paramList.add(appFamilyMember.getStatus());
        }
        String sql = stringBuilder.toString();
        return this.queryObj(sql,paramList,AppFamilyMember.class);
    }

    public AppFamilyMember findByUserId(Integer userid){
        if(StringEx.isNullOrLing(userid)){
            return null;
        }
        List<Object> paramList = new ArrayList<>();
        StringBuilder stringBuilder = new StringBuilder("select * from app_family_member where 1=1");
        stringBuilder.append(" and userid=?");
        paramList.add(userid);
        stringBuilder.append(" and status!=2 and status!=3");
        String sql = stringBuilder.toString();
        return this.queryObj(sql,paramList,AppFamilyMember.class);
    }

    public AppFamilyMember findByUserIdAndFid(Integer userid,Integer fid)
    {
        List<Object> paramList = new ArrayList<>();
        StringBuilder stringBuilder = new StringBuilder("select * from app_family_member where 1=1");
        stringBuilder.append(" and userid=?");
        stringBuilder.append(" and fid=?");
        stringBuilder.append(" and status!=2 and status!=3");
        paramList.add(userid);
        paramList.add(fid);
        String sql = stringBuilder.toString();
        return this.queryObj(sql,paramList,AppFamilyMember.class);
    }


    public AppFamilyMember getFamily(Integer userid){
        if(StringEx.isNullOrLing(userid)){
            return null;
        }
        List<Object> paramList = new ArrayList<>();
        StringBuilder stringBuilder = new StringBuilder("select * from app_family_member where 1=1");
        stringBuilder.append(" and userid=?");
        paramList.add(userid);
        stringBuilder.append(" and status=1");
        String sql = stringBuilder.toString();
        return this.queryObj(sql,paramList,AppFamilyMember.class);
    }

    public AppFamilyMember findById(Integer id){
        if(StringEx.isNullOrLing(id)){
            return null;
        }
        List<Object> paramList = new ArrayList<>();
        StringBuilder stringBuilder = new StringBuilder("select * from app_family_member where 1=1");
        stringBuilder.append(" and id=?");
        paramList.add(id);
        String sql = stringBuilder.toString();
        return this.queryObj(sql,paramList,AppFamilyMember.class);
    }


    /**
     * 根据家族id查询成员，并以成员身份升序查找
     * @param fid
     * @return
     */
    public List<AppFamilyMember> getFamilyMemberByFid(Integer fid)
    {
        if(StringEx.isNullOrLing(fid)){
            return null;
        }
        List<Object> paramList = new ArrayList<>();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("select m.*,i.name as levelname from app_family_member as m join app_family_identity as i where 1=1");
        stringBuilder.append(" and m.fid=?");
        stringBuilder.append(" and m.status = 1");
        stringBuilder.append(" and m.identity=i.id");
        stringBuilder.append(" order by i.sort asc");
//        stringBuilder.append(" limit 5 ");
        paramList.add(fid);
        String sql = stringBuilder.toString();
        return this.queryList(sql,paramList,AppFamilyMember.class);
    }


    public List<AppFamilyMember> getAllFamilyMemberByFid(Integer fid)
    {
        if(StringEx.isNullOrLing(fid)){
            return null;
        }
        List<Object> paramList = new ArrayList<>();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("select m.*,i.name as levelname from app_family_member as m join app_family_identity as i where 1=1");
        stringBuilder.append(" and m.fid=?");
//        stringBuilder.append(" and m.status = 1");
        stringBuilder.append(" and m.identity=i.id");
        stringBuilder.append(" order by i.sort asc");
//        stringBuilder.append(" limit 5 ");
        paramList.add(fid);
        String sql = stringBuilder.toString();
        return this.queryList(sql,paramList,AppFamilyMember.class);
    }

    /**
     * 根据家族id查询待审核家族成员
     * @param fid
     * @return
     */
    public List<AppFamilyMember> getFamilyReviewedMemberByFid(Integer fid)
    {
        if(StringEx.isNullOrLing(fid)){
            return null;
        }
        List<Object> paramList = new ArrayList<>();
        String sql = "select * from app_family_member where fid=? and status=0 order by createtime desc";
        paramList.add(fid);
        return this.queryList(sql,paramList,AppFamilyMember.class);
    }



    public void updateMemberIdentity(Integer id, Integer identityId)
    {
        if(!StringEx.isNullOrLing(id) && !StringEx.isNullOrLing(identityId)){
            String sql = "update app_family_member set identity=? where id=?";
            List<Object> params = new ArrayList<>();
            params.add(identityId);
            params.add(id);
            this.update(sql,params);
        }
    }


    public void updateMemberStatus(Integer id,Integer status)
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "update app_family_member set status=? where id=?";
        list.add(status);
        list.add(id);
        this.update(sql,list);
    }

    public void updateMemberStatus(Integer fid,Integer status,Integer userId)
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "update app_family_member set status=? where userid=? and fid=? and status=1";
        list.add(status);
        list.add(userId);
        list.add(fid);
        this.update(sql,list);
    }

    public void updateMemberDismiss(Integer fid,Integer status)
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "update app_family_member set status=? where fid=?";
        list.add(status);
        list.add(fid);
        this.update(sql,list);
    }

    public List<AppFamilyMember> afterFamilyMemberList(SplitPageBean splitPageBean, AppFamilyMember appFamilyMember)
    {
        ArrayList<Object> list = new ArrayList<>();
        String sql = "select * from app_family_member where 1=1";
        if(StringEx.isNotNull(appFamilyMember.getFamilyid())){
            sql += " and familyid=?";
            list.add(appFamilyMember.getFamilyid());
        }
        if(StringEx.isNotNull(appFamilyMember.getStatus())){
            sql += " and status=?";
            list.add(appFamilyMember.getStatus());
        }
        sql += " order by createtime desc";
        return this.queryList(sql,list,AppFamilyMember.class,splitPageBean);
    }

    public void delByUserId(Integer userId) {
        String sql = "delete from app_family_member where userid="+userId;
        this.del(sql, new ArrayList<>());
    }

    public List<AppFamilyMember> getAllFamilyMemberByFidExistUserId(Integer familyId, Integer userId) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("select * from app_family_member where familyid = ? and userid != ?");
        paramList.add(familyId);
        paramList.add(userId);
        String sql = stringBuilder.toString();
        return this.queryList(sql,paramList,AppFamilyMember.class);
    }
}
